package cn.techaction.controller;

import java.util.List;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.techaction.common.SverResponse;
import cn.techaction.pojo.ActionAddress;
import cn.techaction.pojo.User;
import cn.techaction.service.ActionAddressService;
import cn.techaction.utils.ConstUtil;

@Controller
@RequestMapping("/addr")
public class ActionAddressController {
	@Autowired
    private ActionAddressService aAddrService;
    /**
	 * 新增收货地址
	 * @param user
	 * @return
	 */
	@RequestMapping("/saveaddr.do")
  	@ResponseBody
  	public SverResponse<List<ActionAddress>> saveAddress(HttpSession session,ActionAddress address){
  		User user=(User) session.getAttribute(ConstUtil.CUR_USER);
		if(user==null) {
			return SverResponse.createByErrorMessage("请在登录后操作");
		}
		address.setUid(user.getId());
		//判断是修改还是添加
		SverResponse<String> result=null;
		if(address.getId()==null) {
			result = aAddrService.addAddress(address);
		}else {
			result=aAddrService.updateAddress(address);
		}
		//添加成功，返回当前用户所有地址
		if (result.isSuccess()) {
			return aAddrService.findAddrsByUserId(user.getId());
		}
		return SverResponse.createByErrorMessage(result.getMsg());
  	}
	/**
	 * 删除收货地址
	 * @param session
	 * @param id
	 * @return
	 */
	@RequestMapping("/deladdr.do")
	@ResponseBody
	public SverResponse<List<ActionAddress>> deleteAddress(HttpSession session,Integer id){
		User user=(User) session.getAttribute(ConstUtil.CUR_USER);
		if(user==null) {
			return SverResponse.createByErrorMessage("请在登录后操作");
		}
		//隐性删除
		SverResponse<String> result=aAddrService.delAddress(user.getId(),id);
		//删除成功后，返回当前用户所有地址
		if (result.isSuccess()) {
			return aAddrService.findAddrsByUserId(user.getId());
		}
		return SverResponse.createByErrorMessage(result.getMsg());
		
	}
	/**
	 * 查找登录用户的所有地址信息
	 * @param session
	 * @return
	 */
	@RequestMapping("/findaddress.do")
	@ResponseBody
	public SverResponse<List<ActionAddress>> findAddrs(HttpSession session){
		User user =(User) session.getAttribute(ConstUtil.CUR_USER);
		if(user ==null) {
			return SverResponse.createByErrorMessage("请登录后再进行操作！");
		}
		return aAddrService.findAddrsByUserId(user.getId());
	}
	@RequestMapping("/findaddrs.do")
	@ResponseBody
	public SverResponse<List<ActionAddress>> findAddrss(HttpSession session){
		User user =(User) session.getAttribute(ConstUtil.CUR_USER);
		if(user ==null) {
			return SverResponse.createByErrorMessage("请登录后再进行操作！");
		}
		return aAddrService.findAddrsByUserId(user.getId());
	}
	@RequestMapping("/setdefault.do")
	@ResponseBody
	public SverResponse<List<ActionAddress>> setDefault(HttpSession session,Integer id){
		User user=(User) session.getAttribute(ConstUtil.CUR_USER);
		if(user==null) {
			return SverResponse.createByErrorMessage("请在登录后操作");
		}
		SverResponse<String> result=aAddrService.updateaddrDefaultStatus(user.getId(),id);
		if (result.isSuccess()) {
			return aAddrService.findAddrsByUserId(user.getId());
		}
		return SverResponse.createByErrorMessage(result.getMsg());
		
	}
	
	@RequestMapping("/findadressbyid.do")
	@ResponseBody
	public SverResponse<ActionAddress> findAdressById(Integer id){
		SverResponse<ActionAddress> response = aAddrService.findAddrById(id);
		if (response!=null) {
			return response;
		}
		return SverResponse.createByErrorMessage("错误");
		
	}
}
